package com.zzzy.server.controller;

import com.zzzy.common.util.CglibBeanCopierUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.framework.common.util.SecurityUtils;
import com.zzzy.server.service.LoginService;
import com.zzzy.server.vo.LoginReq;
import com.zzzy.sys.dao.entity.Menu;
import com.zzzy.sys.service.MenuService;
import com.zzzy.sys.vo.user.UserInfoRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: 登录相关
 *
 * @author fc
 * @date 2024/4/16
 */
@Tag(name = "用户登录与用户信息", description = "server001")
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private MenuService menuService;


    @Operation(summary = "获取登录凭证")
    @PostMapping("/login")
    public AjaxResult<String> login(@RequestBody LoginReq loginReq) {
        String token = loginService.login(loginReq);
        return AjaxResult.success(token);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("getUserInfo")
    public AjaxResult<UserInfoRes> getUserInfo() {
        return AjaxResult.success(CglibBeanCopierUtil.copy(SecurityUtils.getLoginUser(), UserInfoRes.class));
    }

    @Operation(summary = "获取菜单信息")
    @PostMapping("/getMenuList")
    public AjaxResult<List<Menu>> login() {
        List<Menu> menuList = menuService.getMenuListByUserId(SecurityUtils.getLoginUser().getId());
        return AjaxResult.success(menuList);
    }

}